import json
from urllib.parse import urlencode

import scrapy


class RequestMethodSpider(scrapy.Spider):
    name = "request_method"

    async def start(self):
        # url = "http://httpbin.org/get?name=wwww"
        # yield scrapy.Request(url, callback=self.parse_get,headers={
        #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/000000000 Safari/537.36 Edg/000000000",
        #
        # },method="GET")


        # yield scrapy.Request(f"http://httpbin.org/get?name=wwww&{urlencode({"id":101,'age': 18})}" ,callback=self.parse_get, headers={
        #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/000000000 Safari/537.36 Edg/000000000",
        #
        # }, method="GET")

        yield scrapy.Request(f"http://httpbin.org/post?name=wwww", method="POST", body=json.dumps({
            "id": 101,
            "age": 18
        }), headers={
            "Content-Type": "application/json",
        })

        # yield scrapy.Request(f"http://httpbin.org/post?name=wwww",method="POST",body=urlencode({
        #     "id":101,
        #     "age":18
        # }),headers={
        #     "Content-Type":"application/x-www-form-urlencoded",
        # })





    def parse_get(self, response):
        print(response.url)
        print(response.json())

    def parse(self, response):
        print(response.url)
        print(response.json())

